<?php

class Schedule_Form extends My_Form {

    private $day;
    private $month;
    private $year;
    private $resource_type_id;

    function __construct($form_id, $day, $month, $year, $resource_type_id) {
        My_Form::__construct($form_id);
        $this->DefaultValues = array('name' => '',
            'address' => '',
            'phone' => '',
            'email' => 'abdulhamid.alsalman@gmail.com',
            'note' => '',
            'action' => 'schedule');
        $this->day = $day;
        $this->month = $month;
        $this->year = $year;
        $this->resource_type_id = $resource_type_id;
        $this->init();
    }

    public function init() {
        $this->form->addDataSource(new HTML_QuickForm2_DataSource_Array($this->DefaultValues));

        $fieldset = $this->form->addElement('fieldset')->setLabel('User Information:');
        $name = $fieldset->addElement('text', 'name', array('size' => 50, 'maxlength' => 30))->setLabel('UserName (letters only):')->addClass('name');
        $email = $fieldset->addElement('text', 'email', array('size' => 50, 'maxlength' => 30))->setLabel('Email:');
        $phone = $fieldset->addElement('text', 'phone', array('size' => 50, 'maxlength' => 15))->setLabel('Phone (numbers only):');
        $address = $fieldset->addElement('textarea', 'address', array('size' => 50, 'maxlength' => 255, 'cols' => 38, 'rows' => 5))->setLabel('Address:');
        $note = $fieldset->addElement('textarea', 'reservation_note', array('size' => 50, 'maxlength' => 500, 'cols' => 38, 'rows' => 7))->setLabel('Reservation Note:');


        $fsGrouped = $this->form->addElement('fieldset')->setLabel('Hours:');
        $boxGroup = $fsGrouped->addElement('group', 'selected_slots')->setLabel('Check at least one:');

        $slot_inventory = SlotDB::getAllSlots();
        if (!is_null($slot_inventory)) {
            $slot_inventory->rewind();
            while ($slot_inventory->valid()) {
                $current_item = new Slot();
                $current_item = $slot_inventory->current();
                $slot_date = $this->year . '-' . $this->month . '-' . $this->day;
                if (isSlotAvailable($current_item->getSlot_id(), $slot_date, $this->resource_type_id)) {
                    $boxGroup->addElement('checkbox', null, array('value' => $current_item->getSlot_id()))->setContent('<span style="color: #00;">' . $current_item->getSlot_time_from() . ' - ' . $current_item->getSlot_time_to() . '</span></br>');
                } else {
                    $boxGroup->addElement('checkbox', null, array('value' => $current_item->getSlot_id(), 'disabled'))->setContent('<span style="color: #00;">' . $current_item->getSlot_time_from() . ' - ' . $current_item->getSlot_time_to() . '</span></br>');
                }
                $slot_inventory->next();
            }
        }


        $boxGroup->addRule('required', 'Check at least one box', 1, HTML_QuickForm2_Rule::ONBLUR_CLIENT_SERVER);

        $name->addFilter('trim')->addRule('required', 'Please enter your username', null, HTML_QuickForm2_Rule::SERVER | HTML_QuickForm2_Rule::CLIENT);
        $email->addFilter('trim')->addRule('required', 'Please enter your email');
        $phone->addFilter('trim')->addRule('required', 'Please enter your phone');
        $address->addFilter('trim')->addRule('required', 'Please enter your address');

        $fieldset->addElement('hidden', 'action')->setValue('schedule');
        $fieldset->addElement('hidden', 'resource_type_id')->setValue($this->resource_type_id);
        $fieldset->addElement('hidden', 'slot_date')->setValue($this->year . '-' . $this->month . '-' . $this->day);
        $fieldset->addElement('hidden', 'slots_group_id')->setValue(uniqid());
        $fieldset->addElement('hidden', 'day')->setValue($this->day);
        $fieldset->addElement('hidden', 'month')->setValue($this->month);
        $fieldset->addElement('hidden', 'year')->setValue($this->year);

        // $fieldset->addElement('submit', null, array('value' => 'Submit'));
        //$fieldset->addElement('button', 'cancelButton', array('onclick' => "window.location.href='index.php'", 'type' => 'button'), array('content' => 'Cancel'));

        $submitGrp = $this->form->addElement('group')->setSeparator('&nbsp;&nbsp;&nbsp;');
        $submitGrp->addElement('submit', 'testSubmit', array('value' => 'Submit'));
        $submitGrp->addElement('button', 'cancelButton', array('onclick' => "window.location.href='index.php'", 'type' => 'button'), array('content' => 'Cancel'));
        /*  $submitGrp->addElement('checkbox', 'clientSide', array('onclick' => 'enableValidation(this);'))
          ->setContent('perform client-side validation')
          ->setAttribute('checked'); // override submit value */
    }

}

?>
